import { createRouter, createWebHistory } from 'vue-router'
import { useTokenStore } from '@/stores/mytoken'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path:"/",
      redirect:"/home"
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('@/views/HomeView.vue'),
      children:[
        {
          path: '/staticPage',
          name: 'staticPage',
          component: () => import('@/views/tabPages/StaticPage.vue')
        },
        {
          path: '/baseOldieReliefType/baseoldierelieftypeclient',
          name: 'baseOldieReliefType/baseoldierelieftypeclient',
          component: () => import ('@/views/tabPages/TestPage01.vue')
        },
        {
          path: '/setUser',
          name: 'setUser',
          component: () => import('@/views/tabPages/SetUser.vue')
        },
        {
          //404页面的路由
          path: '/:pathMatch(.*)*',
          name: 'notFound',
          component: () => import('@/views/tabPages/NotFound.vue')
        }
      ],
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/LoginView.vue')
    },
    {
      path: '/main',
      name: 'main',
      component: () => import('@/views/tabPages/MainPage.vue')
    }
  ]
})

router.beforeEach((to, from) => {
  const store = useTokenStore()
  if (to.name!=="login"&&store.token=="undefined") {
    return { name: 'login'}
  }
})

export default router
